package com.gxa.modules.service.impl;

import com.alibaba.fastjson.JSON;
import com.gxa.common.utils.MapUtils;
import com.gxa.common.vo.JsonResult;
import com.gxa.common.vo.PageRecv;
import com.gxa.modules.mapper.CustomerMapper;
import com.gxa.modules.pojo.Customer;
import com.gxa.modules.pojo.CustomerMap;
import com.gxa.modules.pojo.Customer_LoanCount;
import com.gxa.modules.pojo.Customer_Loan_Loanafter_Bankpay;
import com.gxa.modules.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService{
    @Autowired(required = false)
    private CustomerMapper customerMapper;
    @Override
    public JsonResult queryList(PageRecv pageRecv) {
        Integer param01 = (pageRecv.getCurrentPage()-1)*pageRecv.getPageSize();
        Integer param02 = pageRecv.getCurrentPage()*pageRecv.getPageSize();
        List<Customer_Loan_Loanafter_Bankpay> list =customerMapper.queryList(param01,param02);
        Integer count = customerMapper.count();
        return new JsonResult<>(200,"查询成功",count,list);
    }

    @Override
    public JsonResult specificQueryList(CustomerMap customerMap) {
        Integer param01 = (customerMap.getCurrentPage()-1)*customerMap.getPageSize();
        Integer param02 = customerMap.getCurrentPage()*customerMap.getPageSize();
        String str = JSON.toJSONString(customerMap);
        Map map = MapUtils.delNullMap((Map)JSON.parse(str));
        map.keySet().removeIf(key -> key.equals("currentPage"));
        map.keySet().removeIf(key -> key.equals("pageSize"));
        List<Customer_Loan_Loanafter_Bankpay> list =customerMapper.querspecificQueryListyList(param01,param02,map);
        Integer count = customerMapper.countSpecificQueryList(param01,param02,map);
        JsonResult r = new JsonResult<>(200,"查询成功",count,list);
        return r;
    }

    @Override
    public JsonResult orderSortList(PageRecv pageRecv) {
        Integer param01 = (pageRecv.getCurrentPage()-1)*pageRecv.getPageSize();
        Integer param02 = pageRecv.getCurrentPage()*pageRecv.getPageSize();
        List<Customer_Loan_Loanafter_Bankpay> list =customerMapper.orderSortList(param01,param02);
        Integer count = customerMapper.count();
        return new JsonResult<>(200,"查询成功",count,list);
    }

    @Override
    public JsonResult moneySortList(PageRecv pageRecv) {
        Integer param01 = (pageRecv.getCurrentPage()-1)*pageRecv.getPageSize();
        Integer param02 = pageRecv.getCurrentPage()*pageRecv.getPageSize();
        List<Customer_Loan_Loanafter_Bankpay> list =customerMapper.moneySortList(param01,param02);
        Integer count = customerMapper.count();
        return new JsonResult<>(200,"查询成功",count,list);
    }

    @Override
    public JsonResult timeSortList(PageRecv pageRecv) {
        Integer param01 = (pageRecv.getCurrentPage()-1)*pageRecv.getPageSize();
        Integer param02 = pageRecv.getCurrentPage()*pageRecv.getPageSize();
        List<Customer_Loan_Loanafter_Bankpay> list =customerMapper.timeSortList(param01,param02);
        Integer count = customerMapper.count();
        return new JsonResult<>(200,"查询成功",count,list);
    }

    @Override
    public JsonResult memberGradeSortList(PageRecv pageRecv) {
        Integer param01 = (pageRecv.getCurrentPage()-1)*pageRecv.getPageSize();
        Integer param02 = pageRecv.getCurrentPage()*pageRecv.getPageSize();
        List<Customer_Loan_Loanafter_Bankpay> list =customerMapper.memberGradeSortList(param01,param02);
        Integer count = customerMapper.count();
        return new JsonResult<>(200,"查询成功",count,list);
    }

    @Override
    public JsonResult seeCustomer(long id) {
        Double dou;
        Customer customer = customerMapper.seeCustomer(id);
        Customer_LoanCount customer_loanCount = customerMapper.loanCount(id);
        if(customer_loanCount.getExpectFrequency() == null || customer_loanCount.getExpectFrequency().equals("")){
            dou = 1.00;
        }else{
            dou = (double) 1 - (customer_loanCount.getExpectFrequency() / customer_loanCount.getTotal());
        }
        customer_loanCount.setOnTimeRepayment(dou);
        List<Object> list = new ArrayList<>();
        list.add(customer);
        list.add(customer_loanCount);
        JsonResult<Object> r = new JsonResult<>();
        r.setData(list);
        r.setCode(200);
        return r;
    }

    @Override
    public JsonResult seeCustomerSpecific(long id) {
        Customer customer = customerMapper.seeCustomerSpecific(id);
        JsonResult r = new JsonResult();
        r.setCode(200);
        r.setData(customer);
        return r;
    }
}
